package nl.han.ica.ap.searchengine.client.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

import nl.han.ica.ap.searchengine.client.Engine;
import nl.han.ica.ap.searchengine.client.ResultPage;
import nl.han.ica.ap.searchengine.client.SearchQuery;
import nl.han.ica.ap.searchengine.client.SearchResults;
import nl.han.ica.ap.searchengine.client.controller.SearchEngineController;

public class SearchEngineFrame extends JFrame implements SearchEngineGui {

	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JTextField txtQueryinputfield;
	private JTextPane resultTextPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SearchEngineFrame frame = new SearchEngineFrame(new SearchEngineController(new Engine()));
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SearchEngineFrame(final SearchEngineController controller) 
	{
		controller.setGui(this);
		setTitle("Projectgroep IZA - Realisatie zoekmachine");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
		
		JPanel titlePanel = new JPanel();
		contentPane.add(titlePanel);
		
		JLabel lblZoekmachineProjectgroep = new JLabel("Zoekmachine - Projectgroep IZA");
		titlePanel.add(lblZoekmachineProjectgroep);
		
		JPanel searchInputPanel = new JPanel();
		contentPane.add(searchInputPanel);
		
		txtQueryinputfield = new JTextField();
		searchInputPanel.add(txtQueryinputfield);
		txtQueryinputfield.setColumns(25);
		
		JButton btnSearchWeb = new JButton("Search Database");
		btnSearchWeb.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{	
				//Delegate request to controller
				controller.performSearch(new SearchQuery(txtQueryinputfield.getText()));
			}
		});
		searchInputPanel.add(btnSearchWeb);
		
		resultTextPane = new JTextPane();
		resultTextPane.setEditable(false);
		contentPane.add(resultTextPane);
	}
	
	public void takeResults(SearchResults results)
	{
		resultTextPane.setText("");
		StyledDocument content = resultTextPane.getStyledDocument();
		for (ResultPage page : results.getPages())
		{
			try {
				content.insertString(content.getLength(), page.url + "\n", null);
			} catch (BadLocationException e) {
				e.printStackTrace();
			}
		}
	}

}
